package com.yiyuan.system.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yiyuan.common.exception.SubRecExistException;
import com.yiyuan.system.dao.IEmpDao;
import com.yiyuan.system.dao.IGroupDao;
import com.yiyuan.system.dao.IGroupUserDao;
import com.yiyuan.system.dao.IOrgDao;
import com.yiyuan.system.dao.IResDao;
import com.yiyuan.system.dao.IRoleDao;
import com.yiyuan.system.dao.IRolePriDao;
import com.yiyuan.system.dao.IUserDao;
import com.yiyuan.system.dao.IUserPriDao;
import com.yiyuan.system.dao.IUserRoleDao;
import com.yiyuan.system.model.Employee;
import com.yiyuan.system.model.Group;
import com.yiyuan.system.model.GroupUser;
import com.yiyuan.system.model.Organ;
import com.yiyuan.system.model.PriResource;
import com.yiyuan.system.model.Role;
import com.yiyuan.system.model.RolePri;
import com.yiyuan.system.model.User;
import com.yiyuan.system.model.UserPri;
import com.yiyuan.system.model.UserRole;
import com.yiyuan.system.object.UserObject;
import com.yiyuan.system.service.ISystemService;

@Service("systemService")
public class SystemServiceImpl implements ISystemService {
	@Resource
	private IOrgDao orgDao;
	@Resource
	private IUserDao userDao;
	@Resource
	private IResDao resDao;
	@Resource
	private IRoleDao roleDao;
	@Resource
	private IEmpDao empDao;
	@Resource
	private IUserRoleDao userRoleDao;
	@Resource
	private IUserPriDao userPriDao;
	@Resource
	private IRolePriDao rolePriDao;
	@Resource
	private IGroupDao groupDao;
	@Resource
	private IGroupUserDao groupUserDao;

	/** **********************1������֯���、��组织机构管理******************************** */
	public List<Organ> findOrgList() {
		return this.orgDao.findList();
	}

	public int getPageCount() {
		return this.orgDao.getPageCount();
	}

	public void addOrgan(Organ organ) {
		// 更新上级节点hasChild为1
		if (organ.getOrgPid() != null) {
			Organ pOrgan = this.orgDao.get(organ.getOrgPid());
			if (pOrgan != null) {
				pOrgan.setHasChild("1");
				this.orgDao.update(pOrgan);
			}
		}
		this.orgDao.add(organ);
	}

	public void updateOrgan(Organ organ) {
		this.orgDao.update(organ);
	}

	public void deleteOrgan(Integer orgId) throws SubRecExistException {
		// 1、查询是否有下级机构，有则不能删除
		Organ organ = this.orgDao.get(orgId);		
		if (organ!=null && organ.getHasChild().equals("1")) {
			throw new SubRecExistException();
		}
		// 2、查询是否有平级机构，如果没有则更新上级hasChild为0		
		List<Organ> orgList = this.orgDao.findSubList(organ.getOrgPid()
				.toString());
		if (orgList == null || orgList.size() <= 1) {
			Organ topOrgan = this.orgDao.get(organ.getOrgPid());
			topOrgan.setHasChild("0");
			this.orgDao.update(topOrgan);
		}
		// 3、删除所选机构
		this.orgDao.delete(organ);
	}

	public List<Organ> findSubOrgList(String orgId) {
		return this.orgDao.findSubList(orgId);
	}

	public Organ loadOrgan(Integer orgId) {
		return this.orgDao.get(orgId);
	}

	/** **********************2、用户管理�����û�����*********************************** */
	public List<User> findUserList(int startIndex, int endIndex,Integer deptId,Integer empId) {
		return this.userDao.findList(startIndex, endIndex, deptId,empId);
	}

	public int getUserPageCount(Integer deptId,Integer empId) {
		return this.userDao.getPageCount(deptId,empId);
	}

	public void addUser(User user) {
		String userRoles[] = user.getUserRoles();
		String userPris[] = user.getUserPris();
		this.userDao.add(user);

		for (int i = 0; i < userRoles.length; i++) {
			if (userRoles[i] != null && !userRoles[i].equals("")) {
				UserRole userRole = new UserRole();
				userRole.setUserId(user.getUserId());
				userRole.setRoleId(Integer.valueOf(userRoles[i]));
				this.userRoleDao.add(userRole);
			}
		}
		for (int i = 0; i < userPris.length; i++) {
			if (userPris[i] != null && !userPris[i].equals("")) {
				UserPri userPri = new UserPri();
				userPri.setUserId(user.getUserId());
				userPri.setPriId(Integer.valueOf(userPris[i]));
				this.userPriDao.add(userPri);
			}
		}
	}

	public void updateUser(User user) {
		String userRoles[] = user.getUserRoles();
		String userPris[] = user.getUserPris();
		this.userDao.update(user);

		// 修改用户角色
		UserRole oldUserRole = null;
		boolean flag = false;
		List<UserRole> userRoleList = this.userRoleDao.findListByUserId(user
				.getUserId());
		// 先增新添加的角色
		for (int j = 0; j < userRoles.length; j++) {
			for (int i = 0; i < userRoleList.size(); i++) {
				oldUserRole = userRoleList.get(i);
				if (oldUserRole.getRoleId().intValue() == Integer
						.parseInt(userRoles[j])) {
					flag = true;
					break;
				}
			}
			if (!flag) {
				UserRole newUserRole = new UserRole();
				newUserRole.setUserId(user.getUserId());
				newUserRole.setRoleId(Integer.valueOf(userRoles[j]));
				this.userRoleDao.add(newUserRole);
			}
			flag = false;
		}
		// 删除去掉的角色
		for (int i = 0; i < userRoleList.size(); i++) {
			oldUserRole = userRoleList.get(i);
			for (int j = 0; j < userRoles.length; j++) {
				if (oldUserRole.getRoleId().intValue() == Integer
						.parseInt(userRoles[j])) {
					flag = true;
					break;
				}
			}
			if (!flag) {
				this.userRoleDao.delete(oldUserRole);
			}
			flag = false;
		}

		// 修改用户权限
		UserPri oldUserPri = null;
		List<UserPri> userPriList = this.userPriDao.findListByUserId(user
				.getUserId());
		// 先增新添加的权限
		for (int j = 0; j < userPris.length; j++) {
			for (int i = 0; i < userPriList.size(); i++) {
				oldUserPri = userPriList.get(i);
				if (oldUserPri.getPriId().intValue() == Integer
						.parseInt(userPris[j])) {
					flag = true;
					break;
				}
			}
			if (!flag) {
				UserPri newUserPri = new UserPri();
				newUserPri.setUserId(user.getUserId());
				newUserPri.setPriId(Integer.valueOf(userPris[j]));
				this.userPriDao.add(newUserPri);
			}
			flag = false;
		}
		// 删除去掉的权限
		for (int i = 0; i < userPriList.size(); i++) {
			oldUserPri = userPriList.get(i);
			for (int j = 0; j < userPris.length; j++) {
				if (oldUserPri.getPriId().intValue() == Integer
						.parseInt(userPris[j])) {
					flag = true;
					break;
				}
			}
			if (!flag) {
				this.userPriDao.delete(oldUserPri);
			}
			flag = false;
		}
	}

	public void deleteUser(Integer userId) {
		
		User user = this.userDao.get(userId);
		this.userDao.delete(user);
		
		List<UserRole> userRoleList=this.userRoleDao.findListByUserId(userId);
		for(int i=0;i<userRoleList.size();i++){
			this.userRoleDao.delete(userRoleList.get(i));
		}
		
		List<UserPri> userPriList=this.userPriDao.findListByUserId(userId);
		for(int i=0;i<userPriList.size();i++){
			this.userPriDao.delete(userPriList.get(i));
		}
	}

	public User getUser(Integer userId) {
		return this.userDao.get(userId);
	}

	public List<UserRole> findUserRoleListByUserId(Integer userId) {
		return this.userRoleDao.findListByUserId(userId);
	}

	public List<UserPri> findUserPriListByUserId(Integer userId) {
		return this.userPriDao.findListByUserId(userId);
	}

	public User getUserByNameAndPwd(String userName,String password){
		return this.userDao.getUserByNameAndPwd(userName, password);
	}
	
	public List<UserObject> getUserList(String deptId){
		return this.userDao.getUserList(deptId);
	}
	/** **********************3�����������、权限资源管理******************************** */

	public void addPriResource(PriResource priResource) {
		// 更新上级节点hasChild为1
		if (priResource.getResPid() != null) {
			PriResource pResource = this.resDao.get(priResource.getResPid());
			if (pResource != null) {
				pResource.setHasChild("1");
				this.resDao.update(pResource);
			}
		}
		this.resDao.add(priResource);
	}

	public void updatePriResource(PriResource priResource) {
		this.resDao.update(priResource);
	}

	public void deletePriResource(Integer resId) throws SubRecExistException {
		// 1、查询是否有下级资源，有则不能删除
		List<PriResource> subResList = this.resDao
				.findSubList(resId.toString());
		if (subResList != null && subResList.size() > 0) {
			throw new SubRecExistException();
		}
		// 2、查询是否有同级资源，如果没有则更新上级hasChild为0
		PriResource priResource = this.resDao.get(resId);
		List<PriResource> resList = this.resDao.findSubList(priResource
				.getResPid().toString());
		if (resList == null || resList.size() <= 1) {
			PriResource topPriResource = this.resDao.get(priResource
					.getResPid());
			topPriResource.setHasChild("0");
			this.resDao.update(topPriResource);
		}
		// 3、删除所选资源
		this.resDao.delete(priResource);
	}

	public List<PriResource> findSubPriResList(String resId) {
		return this.resDao.findSubList(resId);
	}

	public List<PriResource> findPriResList() {
		return this.resDao.findList();
	}

	public PriResource loadPriResource(Integer resId) {
		return this.resDao.get(resId);
	}

	/** **********************4、角色管理�����û�����*********************************** */
	public List<Role> findRoleList(int startIndex, int endIndex,String roleName) {
		return this.roleDao.findList(startIndex, endIndex,roleName);
	}

	public int getRolePageCount(String roleName) {
		return this.roleDao.getPageCount(roleName);
	}

	public void addRole(Role role) {
		String rolePris[] = role.getRolePris();
		this.roleDao.add(role);
		for (int i = 0; i < rolePris.length; i++) {
			if (rolePris[i] != null && !rolePris[i].equals("")) {
				RolePri rolePri = new RolePri();
				rolePri.setRoleId(role.getRoleId());
				rolePri.setPriId(Integer.valueOf(rolePris[i]));
				this.rolePriDao.add(rolePri);
			}
		}
	}

	public void updateRole(Role role) {

		String rolePris[] = role.getRolePris();
		this.roleDao.update(role);

		// 修改角色权限
		boolean flag = false;
		RolePri oldRolePri = null;
		List<RolePri> rolePriList = this.rolePriDao.findListByRoleId(role
				.getRoleId());
		// 先增新添加的权限
		for (int j = 0; j < rolePris.length; j++) {
			for (int i = 0; i < rolePriList.size(); i++) {
				oldRolePri = rolePriList.get(i);
				if (oldRolePri.getPriId().intValue() == Integer
						.parseInt(rolePris[j])) {
					flag = true;
					break;
				}
			}
			if (!flag) {
				RolePri newRolePri = new RolePri();
				newRolePri.setRoleId(role.getRoleId());
				newRolePri.setPriId(Integer.valueOf(rolePris[j]));
				this.rolePriDao.add(newRolePri);
			}
			flag = false;
		}
		// 删除去掉的权限
		for (int i = 0; i < rolePriList.size(); i++) {
			oldRolePri = rolePriList.get(i);
			for (int j = 0; j < rolePris.length; j++) {
				if (oldRolePri.getPriId().intValue() == Integer
						.parseInt(rolePris[j])) {
					flag = true;
					break;
				}
			}
			if (!flag) {
				this.rolePriDao.delete(oldRolePri);
			}
			flag = false;
		}

	}

	public void deleteRole(Integer roleId) {
		Role role = roleDao.get(roleId);
		roleDao.delete(role);
		
		List<RolePri> list=this.rolePriDao.findListByRoleId(roleId);
		for(int i=0;i<list.size();i++){
			this.rolePriDao.delete(list.get(i));
		}
	}

	public Role loadRole(Integer roleId) {
		return this.roleDao.get(roleId);
	}

	public List<Role> findRoleList() {
		return this.roleDao.findList();
	}

	public List<RolePri> findRolePriListByRoleId(Integer roleId) {
		return this.rolePriDao.findListByRoleId(roleId);
	}

	/** **********************5、人员管理�����û�����*********************************** */
	public List<Employee> findEmpList(int startIndex, int endIndex,String empName,Integer deptId) {
		return this.empDao.findList(startIndex, endIndex,empName,deptId);
	}

	public int getEmpPageCount(String empName,Integer deptId) {
		return this.empDao.getPageCount(empName,deptId);
	}

	public void addEmp(Employee employee) {
		this.empDao.add(employee);
	}

	public void updateEmp(Employee employee) {
		this.empDao.update(employee);
	}

	public void deleteEmp(Integer empId) {
		Employee employee = empDao.get(empId);
		empDao.delete(employee);
	}

	public Employee loadEmp(Integer empId) {
		return this.empDao.get(empId);
	}

	public List<Employee> getEmpListByDeptId(Integer deptId){
		return this.empDao.findListByDeptId(deptId);
	}
	/** **********************6、组管理�����û�����*********************************** */
	public List<Group> findGroupList(int startIndex, int endIndex,String groupName) {
		return this.groupDao.findList(startIndex, endIndex,groupName);
	}

	public int getGroupPageCount(String groupName) {
		return this.groupDao.getPageCount(groupName);
	}

	public void addGroup(Group group) {
		String groupUsers[] = group.getGroupUsers();
		this.groupDao.add(group);
		for (int i = 0; i < groupUsers.length; i++) {
			if (groupUsers[i] != null && !groupUsers[i].equals("")) {
				GroupUser groupUser = new GroupUser();
				groupUser.setGroupId(group.getGroupId());
				groupUser.setUserId(Integer.valueOf(groupUsers[i]));				
				this.groupUserDao.add(groupUser);
			}
		}
	}

	public void updateGroup(Group group) {

		String groupUsers[] = group.getGroupUsers();
		this.groupDao.update(group);
		// 修改组所包含用户
		boolean flag = false;
		GroupUser oldGroupUser = null;
		List<GroupUser> groupUserList = this.groupUserDao.findListByGroupId(group.getGroupId());
		// 先增新添加的用户
		for (int j = 0; j < groupUsers.length; j++) {
			for (int i = 0; i < groupUserList.size(); i++) {
				oldGroupUser = groupUserList.get(i);
				if (oldGroupUser.getUserId().intValue() == Integer
						.parseInt(groupUsers[j])) {
					flag = true;
					break;
				}
			}
			if (!flag) {
				GroupUser newGroupUser = new GroupUser();
				newGroupUser.setGroupId(group.getGroupId());
				newGroupUser.setUserId(Integer.valueOf(groupUsers[j]));
				this.groupUserDao.add(newGroupUser);
			}
			flag = false;
		}
		// 删除去掉的用户
		for (int i = 0; i < groupUserList.size(); i++) {
			oldGroupUser = groupUserList.get(i);
			for (int j = 0; j < groupUsers.length; j++) {
				if (oldGroupUser.getUserId().intValue() == Integer.parseInt(groupUsers[j])) {
					flag = true;
					break;
				}
			}
			if (!flag) {
				this.groupUserDao.delete(oldGroupUser);
			}
			flag = false;
		}
	}

	public void deleteGroup(Integer groupId) {
		Group group = groupDao.get(groupId);
		groupDao.delete(group);
		
		List<GroupUser> list=this.groupUserDao.findListByGroupId(groupId);
		for(int i=0;i<list.size();i++){
			this.groupUserDao.delete(list.get(i));
		}
	}

	public Group loadGroup(Integer groupId) {
		return this.groupDao.get(groupId);
	}

	public List<Group> findGroupList(Integer userId) {
		return this.groupDao.findList(userId);
	}

	public List<GroupUser> findGroupUserListByGroupId(Integer groupId) {
		return this.groupUserDao.findListByGroupId(groupId);
	}

	public List<GroupUser> findDDList(Integer groupId){
		return this.groupUserDao.findDDList(groupId);
	}

}
